from interview.models import Fact, Question, QuestionSet, QuestionSetList, Rec, Rule, Explanation
from django.contrib import admin

from django import forms
import myValidators


#class RuleInline(admin.StackedInline):
#    model = Rule
#    extra = 1

#class ExplanationInline(admin.StackedInline):
#    model = Explanation
#    extra = 1

#class QuestionInline(admin.StackedInline):
#    model = QuestionSet.question_set.through
#    extra = 3
#    
#class QuestionSetInline(admin.StackedInline):
#    model = QuestionSetList.question_set_list.through
#    extra = 2

class QuestionAdmin(admin.ModelAdmin):
    fields = ['name', 'fact', 'question']
#    inlines = [RuleInline, ExplanationInline]

class QuestionSetAdminForm(forms.ModelForm):
    class Meta:
        model = QuestionSet

    def clean(self):
        myValidators.validate_when(self.cleaned_data.get('fact1'), self.cleaned_data.get('fact2'), self.cleaned_data.get('fact3'), self.cleaned_data.get('logical_operator1'), self.cleaned_data.get('logical_operator2'))
        myValidators.validate_QuestionSet(self.cleaned_data.get('name'), self.cleaned_data.get('question_sets_for_questions'), self.cleaned_data.get('question_sets_for_question_sets'))
        return self.cleaned_data

class QuestionSetAdmin(admin.ModelAdmin):
    fieldsets = (
                 (None, {
                         'fields': ('name', 'question_sets_for_questions', 'question_sets_for_question_sets')
                }),
                 ('When',{
                         'fields': ('fact1', 'logical_operator1', 'fact2', 'logical_operator2', 'fact3')
                 }),
    )
    
    form = QuestionSetAdminForm  
    
#    fields = ['name', 'fact1', 'logical_operator1', 'fact2', 'logical_operator2', 'fact3', 'question_set_for_questions', 'question_set_for_question_sets']
    #inlines = [QuestionInline]

class QuestionSetListAdmin(admin.ModelAdmin):
    fields = ['name', 'question_sets_list']
    #inlines = [QuestionSetInline]

class RuleAdmin(admin.ModelAdmin):
    fields = ('fact1', 'logical_operator1', 'fact2', 'logical_operator2', 'fact3', 'rec1', 'rec2')

admin.site.register(Fact)
admin.site.register(Question, QuestionAdmin)
admin.site.register(QuestionSet, QuestionSetAdmin)
admin.site.register(QuestionSetList, QuestionSetListAdmin)
admin.site.register(Rec)
admin.site.register(Rule, RuleAdmin)
admin.site.register(Explanation)